package com.example.commonlib.httplib.response;

import android.content.Context;
import android.net.ParseException;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.commonlib.base.BaseView;
import com.google.gson.JsonParseException;

import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.HttpException;


/**
 * Create by xjs
 * _______date : 17/8/8
 * _______description:http请求错误处理
 */
public enum HttpFailResponse {
    INSTANCE;

    //对应HTTP的状态码
    private static final int SUCCESS = 200;
    private static final int BADREQUEST = 400;
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int INTERNAL_SERVER_ERROR = 500;

    private static final int PARSE_ERROR = 1001;

    private final String parseMsg = "数据解析失败";


    public String exceptionInfo(Throwable e, Context context, BaseView view) {
        Throwable throwable = e;
        while (throwable.getCause() != null) {
            e = throwable;
            throwable = e.getCause();
        }
        String result = null;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            String errorBody = getErrorBody(httpException, context);
            switch (httpException.code()) {
                case SUCCESS:{
                    result = displayMessage(httpException.code(), httpException.message());
                    break;
                }
                case UNAUTHORIZED:
                    if (view != null) {
                        view.tokenFailed();
                    }
                    result = displayMessage(UNAUTHORIZED, "身份认证失败，请重新登录");
                    //TODO 清空所有的登入信息
//                    Intent intent = new Intent((Context) view, LoginActivity.class);
//                    App.INSTANCE.startActivity(intent);
                    break;
                case BADREQUEST:
                    result = displayMessage(httpException.code(), errorBody);
                    break;
                case FORBIDDEN:
                    result = displayMessage(httpException.code(), errorBody);
                    break;
                case NOT_FOUND:

                    break;
                case INTERNAL_SERVER_ERROR:
                    result = displayMessage(httpException.code(), errorBody);
                    break;

                default:
                    result = displayMessage(httpException.code(), "网络错误");   //其他都视为网络错误
                    break;
            }
        } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
            result = displayMessage(PARSE_ERROR, parseMsg);
        }
        throwable.printStackTrace();
        if (TextUtils.isEmpty(result)) {
            result = throwable.getMessage();
        }

        if (result.contains("DOCTYPE HTML PUBLIC")) {
            //result = "请求失败";
            result = "网络错误";
        }
        return result;
    }

    @Nullable
    private String getErrorBody(HttpException httpException, Context context) {
        String errorBody = null;
        String errorData;
        JSONObject jsonObject = null;
        try {
            errorData = httpException.response().errorBody().string();
            if (errorData.startsWith("{") && errorData.endsWith("}")) {
                jsonObject = new JSONObject(errorData);
            } else {
                errorBody = errorData;

            }
            if (jsonObject != null) {
                if (jsonObject.has("error_msg")) {
                    errorBody = jsonObject.getString("error_msg");
                } else if (jsonObject.has("error_code")) {
                    errorBody = jsonObject.getString("error_code");
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return errorBody;
    }

    private String displayMessage(int code, String msg) {
        return msg;
    }

}
